package com.jcx.ldzj.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.jcx.ldzj.emun.MyCode;
import com.jcx.ldzj.entity.SystemHardwareInfo;
import com.jcx.ldzj.service.IBaseService;
import com.jcx.ldzj.service.IGenTableCretear;
import com.jcx.ldzj.utile.RandomValidateCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ld
 * @since 2020-12-08
 */
@RestController
@Slf4j
@AllArgsConstructor
@RequestMapping("/system")
@Api(value = "读取系统数据的接口", tags = "读取系统数据的接口")
public class BaseController {


    @Autowired
    private IBaseService iBaseService;
    @Autowired
    private IGenTableCretear iGenTableCretear;
    /**
     *
     * @return
     */
    @GetMapping("/getSystem")
    @ApiOperation(value = "详情", notes = "传入id")
    @RequiresPermissions("basic:system:getSystem")
    public R<SystemHardwareInfo> view() {
        return R.ok(iBaseService.getSystem());
    }

    /**
     * 未授权容错接口
     * @return
     */
    @RequestMapping("/noauth")
    public R noauth() {
        return R.failed(MyCode.该接口未授权_请联系管理员);
    }


    /*
     * 登录页面生成验证码
     */
    @RequestMapping(value = "/getVerify")
    @ApiOperation(value = "生成图片验证码")
    public void getVerify(HttpServletRequest request, HttpServletResponse response){
        response.setContentType("image/jpg");//设置相应类型,告诉浏览器输出的内容为图片
        response.setHeader("Pragma", "No-cache");//设置响应头信息，告诉浏览器不要缓存此内容
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        try {
            RandomValidateCode.getRandcode(request, response);//输出验证码图片方法
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * 登录页面生成验证码
     */
    @GetMapping(value = "/getEmailCode")
    @ApiOperation(value = "发送验证码", notes = "用户名，邮箱号")
    public R getEmailCode(HttpServletRequest request,@RequestParam String userName,@RequestParam String myeMail){
        return iGenTableCretear.getEmailCode(request,userName,myeMail);
    }
}
